/***************************************************************************
 *   Copyright (C) 2007 by Anders Nore																	   *
 *   andenore@start.no   												   												 *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#include "duxinterface.h"
#include "mcastchat.h"

void refreshChatBar(WINDOW *chatwin);

WINDOW* setupMainWindow(WINDOW *mainwin) {	
	raw();
	noecho();
	wsetscrreg(mainwin, 5, 19);			  /* setter scrolling region (top, bottom) */
	scrollok(mainwin, TRUE);	/* enabla scrolling i ditta vindauge */
	idlok(mainwin, TRUE);
	keypad(mainwin, FALSE);
	printw("Velkommen til mcastchat v0.1\t\t\tCopyright\xA9 by Anders Nore\nType /quit to quit\n", maxx, maxy);
	hline(ACS_HLINE, maxx);
	wmove(mainwin, 6, 0);
	wrefresh(mainwin);
	
	return 0;
}

WINDOW *create_window(int width, int height, int startx, int starty)
{
	WINDOW *local_window;
	local_window = newwin(height, width, starty, startx);
		
	return local_window;
}

WINDOW *setupChatBar(void)
{
	static WINDOW *chatbar; 
	chatbar = create_window(maxx, 3, 0, maxy - 4);
	wattron(chatbar, A_BOLD);		// hightlighta tekst
	keypad(chatbar, TRUE);
	
	return chatbar;
}

int destroy_window(WINDOW *local_win)
{
	wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
	wrefresh(local_win);
	delwin(local_win);
	
	return 0;
}

void refreshChatBar(WINDOW *chatbar)
{
	
	wmove(chatbar, 1, 1);
	whline(chatbar, ACS_HLINE, maxx);
	wmove(chatbar, 2, 1);
	wrefresh(chatbar);
	
	return;
}

/* Initialize ncurses colors */
int initializeColors(void)
{
	if(enable_colors) {
		start_color();
		init_pair(COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK); /* default color */
		init_pair(COLOR_COMMAND, COLOR_GREEN, COLOR_BLACK);	/* command color */
		init_pair(COLOR_SYSTEM, COLOR_CYAN, COLOR_BLACK);	/* system color */
	}
}

void setColor(WINDOW *win, int color)
{
	if(enable_colors)
		wattron(win, COLOR_PAIR(color));
}

/* Prints important system messages to mainwin */
int SYSPRINT(char *fmt, ...)
{
	char buf[MAX_LINE + 1];
	va_list ap;
	va_start(ap, fmt);
	
	vsnprintf((char *)&buf, MAX_LINE, fmt,	ap);
	setColor(mainwin, COLOR_SYSTEM);
	waddstr(mainwin, "SYSTEM: ");
	wprintw(mainwin, buf);
	waddstr(mainwin, "\n");
	wrefresh(mainwin);
	setColor(mainwin, COLOR_DEFAULT);
	
	va_end(ap);
	
	return 0;
}
